<?php

namespace App\Http\Controllers\Admin;
use \Hash;
use \DB;
use Illuminate\Http\Request;
use App\Http\Requests\CateInsertRequest;
use App\Http\Requests\CateUpdateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{

	//获取主页
	  public function getIndex(Request $request)
	  {
	  	//判断是否传页码
	  	// $lists['num']=$request->session()->get('num',3);
	  	// $lists['page']=$request->session()->get('page',1);
	  	$lists['num'] = $request->input('num',3);
	  	$lists['page'] = $request->input('page',1); 
	  	$lists['keyword'] = $request->input('keyword',''); 
	  	// //判断查询条件是否为空
	  	if(!empty($request->input('keyword')))
	  	{
	  	// $cates = DB::table('shop_type')->where('tname','like','%'.$request->input('keyword').'%')->paginate($num);
	  	   $cates = DB::table('shop_type')->select(DB::raw(' *,concat(path,tid) as paths'))->where('tname','like','%'.$request->input('keyword').'%')->orderBy('paths')->paginate($lists['num']);
	  		
	  	}else{
	  	// $cates = DB::table('shop_type')->paginate($num);
	  		$cates = DB::table('shop_type')->select(DB::raw(' *,concat(path,tid) as paths'))->orderBy('paths')->paginate($lists['num']);
	  		// dd($cates);
	  	}
	  
	  	//判断$lists有没有page
	  	// $cates = DB::select('select *,concat(path,",",tid) as paths     from shop_type order by paths');
	  	// dd($lists);
	  	//获取所有的数据
	  	// $cates = DB::table('shop_type')->get();
	  	// dd($cates);
	  	//进行遍历
	  	foreach ($cates as $k => $v) {
	  		  $str = rtrim($v->path,',');
	  		  $arr = 	explode(',', $str);
	  		  $len = count($arr)-1;
	  		  $v->tname = str_repeat('---|',$len).$v->tname;
	  	}

	  	//解析模板
	  	return view('admin.cate.index',['cates'=>$cates,'lists'=>$lists]);	

	  }
	//添加数据
	  public function getAdd()
	  {
	  	//连接数据库查询所有数据
	  	$cates = DB::select('select *,concat(path,tid) as paths from shop_type order by paths');

	  	// dd($cates);
	  	foreach ($cates as $k => $v) {
	  		$str = rtrim($v->path,',');
	  		$arr = explode(',', $str);
	  		$len = count($arr)-1;
	  		$v->tname= str_repeat('---|', $len).$v->tname;
	  	}
		//解析模板
		return view('admin.cate.add',['cates'=>$cates]);	

	  }
	//处理提交过来的数据
	  public function postDoadd(Request $request)
	  {
	  	 $data = $request->only(['pid','tname']);
	  	 // dd($data);
	  	// dd($cate);
	  	 //图片处理
	  	 if($request->hasFile('tpic'))
	  	 {
	  	 	//获取拓展名
	  	 	$ex = $request->file('tpic')->getClientOriginalExtension();
	  	 	//拼接新的文件名
	  	 	$newname = md5(time().rand(1,9999));
	  	 	//进行移动
	  	 	$request->file('tpic')->move('./upload/cate/',$newname.'.'.$ex);
	  	 	$data['tpic'] = '/upload/cate/'.$newname.'.'.$ex;

	  	 }
	  	 //进行判断拼接路径
	  	 if($data['pid']==0)
	  	 {
	  	 	$data['path']='0,';
	  	 }else{
	  	 //连接数据库
	  	    $cate=DB::table('shop_type')->where('tid',$data['pid'])->first();
	  	 	$data['path']=$cate->path.$cate->tid.',';
	  	 }
	  	 //执行添加
	  	 $res=DB::table('shop_type')->insert($data);
	  	 if($res)
	  	 {
	  	 	return redirect('./admin/cate/index')->with('success','添加类别成功');
	  	 }else{
	  	 	return back()->with('error','添加类别失败');

	  	 }
	  }
	//进行删除操作
	  public function postDelete(Request $request)
	  {
	  	//获取id
	  	$id = $request->input('uid');
	  	// echo $id;
	  	// dd($request);
	  	//查询父id为6的数据
	  		$re = DB::table('shop_type')->where('pid',$id)->get();
	  	//判断
	  		if(!$re)
	  			{
	  				//执行删除操作
	  				$res = DB::table('shop_type')->where('tid',$id)->delete();
	  				if($res)
	  				{
	  					echo 1;
	  				}else{
	  					echo 2;
	  				}
	  			}else{
	  					echo 3;
	  			}

	  }
	//进行修改操作
	  public function getEdit(Request $request)
	  {
	  	//获取id
	  	$page = $request->input('page'); 
	  	$num = $request->input('num');
	  	$k = $request->input('keyword');
	  	$request->session()->put('page',$page);
	  	$request->session()->put('num',$num);
	  	$request->session()->put('keyword',$k);
	  	//获取id进行查询
	  	$id = $request->input('id');
	  	$request->session()->put('id',$id);

	  	// 判断查询
	  	 $re = DB::table('shop_type')->where('pid',$id)->get();
	  	
	  	

	  	//查询所有的类别							
	  	$cates = DB::table('shop_type')->select(DB::raw('*,concat(path,tid) as paths'))->orderBy('paths')->get();
	  	//对cates进行处理
	  	foreach($cates as $k => $v) {
	  		$str = rtrim($v->path,',');
	  		$arr = explode(',', $str);
	  		$len = count($arr)-1;
	  		$v->tname = str_repeat('---|',$len).$v->tname;
	  	}
	  	$cate = DB::table('shop_type')->where('tid',$id)->first();
	  	// dd($cate);
	  		return view('admin.cate.edit',['cate'=>$cate,'cates'=>$cates,'res'=>$re]);
	  }
	 //处理修改的数据
	  public function postDoedit(Request $request)
	  {
	  	 $value = $request->session()->get('id');
	  	 $page = $request->session()->get('page');
	  	 $num = $request->session()->get('num');
	  	 $k = $request->session()->get('keyword');
	  
	  	 
	  	
		  	 // if(!$re)
		  	 // {
		  	 // 	$data = $request->only(['tname']);
		  	 // }
	  	// dd($request->all());
	  	// $page = $request->input('page');
	  	// $num = $request->input('num');
	  	//获取数据
	  	$data = $request->only(['pid','tname']);
	  	// dd($data);
	  	//拼接path
	  	if($data['pid']==0)
	  	{
	  		$data['path']='0,';
	  	}else{
	  		$res = DB::table('shop_type')->where('tid',$data['pid'])->first();
	  		$data['path']= $res->path.$data['pid'].',';
	  	}
	  	//执行修改操作
	  	$res = DB::table('shop_type')->where('tid',$value)->update($data);
	  	//进行判断
	  	if($res)
	  	{
	  		return redirect('/admin/cate/index?page='.$page.'&num='.$num.'&keyword='.$k)->with('success','类别修改成功');
	  	}else{
	  		return back()->with('error','类别修改失败');
	  	}
	  }



}
